﻿using Duality;
using Duality.Components;
using Duality.Components.Renderers;
using OpenTK;

namespace ClientApp
{
	[RequiredComponent(typeof(Transform))]
	[RequiredComponent(typeof(TextRenderer))]
	public class SpellComment : Component, ICmpInitializable, ICmpUpdatable
	{
		private const float LifeTimeBase = 2000.0f;
		private float lifeTime = LifeTimeBase;
		private float creationTime;
		private int rating;
		private PlayerIdentity player;

		public float Progress
		{
			get { return (Time.GameTimer - creationTime) / lifeTime; }
		}
		public int Rating
		{
			get { return this.rating; }
			set { this.rating = value; this.UpdateRatingText(); }
		}
		public PlayerIdentity PlayerId
		{
			get { return this.player; }
			set { this.player = value; this.UpdatePlayerColor(); }
		}

		public void OnInit(Component.InitContext context)
		{
			creationTime = Time.GameTimer;
		}
		public void OnShutdown(Component.ShutdownContext context)
		{
		}
		void ICmpUpdatable.OnUpdate()
		{
			TextRenderer renderer = this.GameObj.GetComponent<TextRenderer>();
			Transform transform = this.GameObj.Transform;

			float progress = this.Progress;
			if (progress > 1.0f)
			{
				this.GameObj.DisposeLater();
			}
			else
			{
				renderer.ColorTint = renderer.ColorTint.WithAlpha(1.0f - progress);
			}
		}
		private void UpdateRatingText()
		{
			TextRenderer renderer = this.GameObj.GetComponent<TextRenderer>();
			string text = "Rating";
			switch (this.rating)
			{
				case 0: text = "EPIC FAIL"; break;
				case 1: text = "Poor"; break;
				case 2: text = "Okay"; break;
				case 3: text = "Good"; break;
				case 4: text = "Great!"; break;
				case 5: text = "Amazing!"; break;
				case 6: text = "EPIC WIN"; break;
			}
			renderer.Text.ApplySource(text);
			renderer.UpdateMetrics();
		}
		private void UpdatePlayerColor()
		{
			TextRenderer renderer = this.GameObj.GetComponent<TextRenderer>();
			renderer.ColorTint = this.player.GetUnitColor();
		}
	}
}
